package com.example.demo.controller.client;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.ArticleDao;
import com.example.demo.model.CategoryModel;
import com.example.demo.model.FriendsLinkModel;
import com.example.demo.model.LabelModel;
import com.example.demo.services.*;
import com.github.pagehelper.PageInfo;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.ui.Model;
import com.example.demo.model.ArticleModel;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.jms.Queue;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

@RestController
public class HomeController {

    @Resource
    CategoryService categoryService;
    @Resource
    ArticleService articleService;
    @Resource
    ArticleDao articleDao;
    @Resource
    LabelService labelService;
    @Resource
    CliectService cliectService;
    @Resource
    JmsMessagingTemplate jmsMessagingTemplate;
    @Resource
    FriendsLinkService friendsLinkService;

    @ModelAttribute
    public void commonModelAttribute(Model model) {
        List<FriendsLinkModel> friendsLinkList = friendsLinkService.getAllFriendsLinkList("sort desc");
        model.addAttribute("friendsLinkList", friendsLinkList);
    }

    /**
     * 首页
     * @param model
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping("/")
    public ModelAndView home(
            Model model,
            @RequestParam(name = "page", required = false, defaultValue = "1") int page,
            @RequestParam(name = "l", required = false, defaultValue = "0") int labelId,
            @RequestParam(name= "c", required = false, defaultValue = "0") int categoryId
    ) throws Exception {

        // 获取当前页的文章数据
        PageInfo pageInfo = articleService.getArticleVisitPageList(page, labelId, categoryId);

        // 获取最新的六篇文章
        PageInfo newestSixArticleList = articleService.getArticlePageList(1, 6);

        // 获取最热门的十个标签
        List<LabelModel> recommendLabelList = labelService.getRecommendLabelList();

        // 获取文章分类
        List<CategoryModel> categoryList = categoryService.getCategoryList();

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("newestSixArticleList", newestSixArticleList);
        model.addAttribute("recommendLabelList", recommendLabelList);
        model.addAttribute("categoryList", categoryList);
        // 把参数传回模板
        model.addAttribute("labelId", labelId);
        model.addAttribute("categoryId", categoryId);

        return new ModelAndView("client/index");
    }

    /**
     * 文章详情页
     * @param model
     * @param request
     * @param articleId
     * @return
     * @throws Exception
     */
    @RequestMapping("detail")
    public ModelAndView detail(
            Model model,
            HttpServletRequest request,
            @RequestParam(value = "id", required = false, defaultValue = "0") int articleId
    ) throws Exception{

        // 获取当前文章数据
        ArticleModel articleInfo = articleDao.getArticleInfoById(articleId);

        // 获取最新的六篇文章
        PageInfo newestSixArticleList = articleService.getArticlePageList(1, 6);

        // 获取最热门的十个标签
        List<LabelModel> recommendLabelList = labelService.getRecommendLabelList();

        // 获取文章分类
        List<CategoryModel> categoryList = categoryService.getCategoryList();

        // 创建一个名字为ArticleDetailVisitQueue的队列
        Queue articleQueue = new ActiveMQQueue("ArticleDetailVisitQueue");
        // 获取客户端IP
        String clientIp = cliectService.getCliectIp(request);
        // 把数据写入到一个新的列表
        Map<String, Object> sendMQMessage = new HashMap<String, Object>();
        sendMQMessage.put("clientIp", clientIp);
        sendMQMessage.put("articleId", articleId);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        // new Date()为获取当前系统时间
        sendMQMessage.put("visitDateTime", df.format(new Date()));
        String sendMQMessageString = JSONObject.toJSONString(sendMQMessage);
        // 推送消息到MQ
        jmsMessagingTemplate.convertAndSend(articleQueue, sendMQMessageString);

        model.addAttribute("articleInfo", articleInfo);
        model.addAttribute("newestSixArticleList", newestSixArticleList);
        model.addAttribute("recommendLabelList", recommendLabelList);
        model.addAttribute("categoryList", categoryList);

        return new ModelAndView("client/detail");

    }

}
